package com.hatic.purchase;

import java.util.Map;

import org.jxstar.control.action.RequestContext;
import org.jxstar.dao.DaoParam;
import org.jxstar.service.BusinessObject;
import org.jxstar.util.DateUtil;
import org.jxstar.util.key.CodeCreator;
import org.jxstar.util.key.KeyCreator;

/**
 * 招标采购方案申请事件处理
 * @author caojiaxin
 * @version 1.0,2011-10-27
 */
public class PurApplyCHKBO extends BusinessObject{

	private static final long serialVersionUID = 1L;

	/**
	 * 招标方案申请审批通过后，产生一条招标采购单记录
	 * @param chkId
	 * @return
	 */
	public String onChkPass(String chkId){
		if(chkId == null || chkId.equals("")){
			_log.showDebug("没有获取到招标方案主键");
			return _returnFaild;
		}
		if(!createPurApply(chkId)){
			_log.showDebug("创建招标采购单失败");
			return _returnFaild;
		}
		return _returnSuccess;
	}

	/**
	 * 创建招标采购单
	 * @param chkId
	 * @return
	 */
	private boolean createPurApply(String chkId) {
		String querySql="select project_id,project_code,project_name,money_type,project_user,auth_type,"+
		" project_userid,check_fileno,check_name,project_money,plan_money,dept_id,dept_name,done_deptid,done_deptname,pur_type,"+
		" org_type,org_type_proxy,apply_userid,apply_user,apply_date,project_content,add_userid,apply_chkid from pur_apply_chk where apply_chkid = ?";
		
		_log.showDebug("==============chkId='"+chkId+"';sql="+querySql);
		
		DaoParam qparam = _dao.createParam(querySql);
		qparam.addStringValue(chkId);
		Map<String, String> qmap=_dao.queryMap(qparam);
		
		String applyId=KeyCreator.getInstance().createKey("pur_apply");
		String applyCode=CodeCreator.getInstance().createCode("pur_apply");
		
		String createSql="insert into pur_apply(apply_id,apply_code,project_id,project_code, "+
		" project_name,money_type,project_user,project_userid,check_fileno,check_name,project_money,plan_money,dept_id,dept_name,done_deptname,"+
		" done_deptid,pur_type,org_type,proxy_org,apply_status,apply_userid,apply_user,apply_date,project_content,auditing,add_userid,add_date ,check_id ,apply_name,auth_type)"+
		" values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";
		
		_log.showDebug("==============sql="+createSql);
		
		DaoParam cparam = _dao.createParam(createSql);
		cparam.addStringValue(applyId);
		cparam.addStringValue(applyCode);
		cparam.addStringValue(qmap.get("project_id"));
		cparam.addStringValue(qmap.get("project_code"));
		cparam.addStringValue(qmap.get("project_name"));
		cparam.addStringValue(qmap.get("money_type"));
		cparam.addStringValue(qmap.get("project_user"));
		cparam.addStringValue(qmap.get("project_userid"));
		cparam.addStringValue(qmap.get("check_fileno"));
		cparam.addStringValue(qmap.get("check_name"));
		cparam.addDoubleValue(qmap.get("project_money"));
		cparam.addDoubleValue(qmap.get("plan_money"));
		cparam.addStringValue(qmap.get("dept_id"));
		cparam.addStringValue(qmap.get("dept_name"));
		cparam.addStringValue(qmap.get("done_deptname"));
		cparam.addStringValue(qmap.get("done_deptid"));
		cparam.addStringValue(qmap.get("pur_type"));
		cparam.addStringValue(qmap.get("org_type"));
		cparam.addStringValue(qmap.get("org_type_proxy"));
		cparam.addStringValue("1");
		cparam.addStringValue(qmap.get("apply_userid"));
		cparam.addStringValue(qmap.get("apply_user"));
		cparam.addDateValue(qmap.get("apply_date"));
		cparam.addStringValue(qmap.get("project_content"));
		cparam.addStringValue("0");
		cparam.addStringValue(qmap.get("add_userid"));
		cparam.addDateValue(DateUtil.getToday());
		cparam.addStringValue(chkId);
		cparam.addStringValue(qmap.get("project_name"));
		cparam.addStringValue(qmap.get("auth_type"));
		
		 if(!_dao.update(cparam)){
			 return false;
		 }
		 return updateApplyID(applyId, applyCode, chkId);
	}
	
	/**
	 * 创建新招标采购单
	 * @param request
	 * @return
	 */
	public String createPurApply2(RequestContext request) {
		//String chkId = request.getRequestValue("pur_apply_chk.apply_chkid");
		String apply_name = request.getRequestValue("pur_apply.apply_name");
		String[] chkIds = request.getRequestValues("keyid");
		if (chkIds == null || chkIds.length == 0) {
			setMessage("没选择需要授权的投资计划！");
			return _returnFaild;
		}
		String chkId = chkIds[0];
		
		String querySql="select project_id,project_code,project_name,money_type,project_user,auth_type,"+
		" project_userid,check_fileno,check_name,project_money,plan_money,dept_id,dept_name,done_deptid,done_deptname,pur_type,"+
		" org_type,org_type_proxy,apply_userid,apply_user,apply_date,project_content,add_userid,apply_chkid from pur_apply_chk where apply_chkid = ?";
		
		_log.showDebug("==============chkId='"+chkId+"';sql="+querySql);
		
		DaoParam qparam = _dao.createParam(querySql);
		qparam.addStringValue(chkId);
		Map<String, String> qmap=_dao.queryMap(qparam);
		
		String applyId=KeyCreator.getInstance().createKey("pur_apply");
		String applyCode=CodeCreator.getInstance().createCode("pur_apply");
		
		String createSql="insert into pur_apply(apply_id,apply_code,project_id,project_code, "+
		" project_name,money_type,project_user,project_userid,check_fileno,check_name,project_money,plan_money,dept_id,dept_name,done_deptname,"+
		" done_deptid,pur_type,org_type,proxy_org,apply_status,apply_userid,apply_user,apply_date,project_content,auditing,add_userid,add_date ,check_id ,apply_name,auth_type)"+
		" values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";
		
		_log.showDebug("==============sql="+createSql);
		
		DaoParam cparam = _dao.createParam(createSql);
		cparam.addStringValue(applyId);
		cparam.addStringValue(applyCode);
		cparam.addStringValue(qmap.get("project_id"));
		cparam.addStringValue(qmap.get("project_code"));
		cparam.addStringValue(qmap.get("project_name"));
		cparam.addStringValue(qmap.get("money_type"));
		cparam.addStringValue(qmap.get("project_user"));
		cparam.addStringValue(qmap.get("project_userid"));
		cparam.addStringValue(qmap.get("check_fileno"));
		cparam.addStringValue(qmap.get("check_name"));
		cparam.addDoubleValue(qmap.get("project_money"));
		cparam.addDoubleValue(qmap.get("plan_money"));
		cparam.addStringValue(qmap.get("dept_id"));
		cparam.addStringValue(qmap.get("dept_name"));
		cparam.addStringValue(qmap.get("done_deptname"));
		cparam.addStringValue(qmap.get("done_deptid"));
		cparam.addStringValue(qmap.get("pur_type"));
		cparam.addStringValue(qmap.get("org_type"));
		cparam.addStringValue(qmap.get("org_type_proxy"));
		cparam.addStringValue("1");
		cparam.addStringValue(qmap.get("apply_userid"));
		cparam.addStringValue(qmap.get("apply_user"));
		cparam.addDateValue(qmap.get("apply_date"));
		cparam.addStringValue(qmap.get("project_content"));
		cparam.addStringValue("0");
		cparam.addStringValue(qmap.get("add_userid"));
		cparam.addDateValue(DateUtil.getToday());
		cparam.addStringValue(chkId);
		cparam.addStringValue(apply_name);
		cparam.addStringValue(qmap.get("auth_type"));
		
		 if(!_dao.update(cparam)){
			 _log.showDebug("创建招标采购单失败");
			 return _returnFaild;
		 }
		 return _returnSuccess;
	}
	/**
	 * 把apply_id,apply_code反馈到方案申请表
	 * @param applyid
	 * @param applycode
	 * @param chkid
	 * @return
	 */
	private boolean updateApplyID(String applyid,String applycode,String chkid){
		String sql="update pur_apply_chk set apply_id = ? , apply_code = ? where apply_chkid = ?";
		_log.showDebug("--------------update pur_apply_chk, apply_chkid="+chkid+";sql="+sql);
		
		DaoParam param=_dao.createParam(sql);
		param.addStringValue(applyid);
		param.addStringValue(applycode);
		param.addStringValue(chkid);
		
		return _dao.update(param);
	}
	
	
}
